package com.yict.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.ListUtils;

import com.yict.common.exception.ServiceException;

/**
 * <h3>Class name</h3> 数组工具类 <h4>Description</h4> <h4>Special Notes</h4>
 * 
 * @ver 0.1
 * @author Jay.Wu 2008-7-29
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ListUtil extends ListUtils {

	/**
	 * 判断collection是否为空
	 * 
	 * @param collection
	 * @return
	 */

	public static boolean isEmpty(Collection collection) {
		if (collection == null || collection.isEmpty()) {
			return true;
		}

		return false;
	}

	/**
	 * 判断数组是否为空
	 * 
	 * @param objects
	 * @return
	 */
	public static boolean isEmpty(Object[] objects) {
		if (objects == null || objects.length == 0) {
			return true;
		}

		return false;
	}

	/**
	 * 判断数组是否不为空
	 * 
	 * @param objects
	 * @return
	 */
	public static boolean isNotEmpty(Object[] objects) {
		return !isEmpty(objects);
	}

	/**
	 * 判断collection是否非空
	 * 
	 * @param collection
	 * @return
	 */
	public static boolean isNotEmpty(Collection collection) {
		return !isEmpty(collection);
	}

	/**
	 * 删除list集合中重复值
	 * 
	 */
	public static List removeDuplicateBySet(List source) {
		Set set = new HashSet();
		List distinctResult = new ArrayList();
		for (Iterator iter = source.iterator(); iter.hasNext();) {
			Object element = iter.next();
			if (set.add(element))
				distinctResult.add(element);
		}
		source.clear();
		source.addAll(distinctResult);
		return source;
	}

	/**
	 * 将数组转换为list
	 * 
	 * @param array
	 * @return
	 */
	public static List Array2List(Object[] array) {
		List list = new ArrayList();
		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				list.add(array[i]);
			}
		}
		return list;
	}

	public static Object unique(List<?> list) {
		if (isNotEmpty(list)) {
			return list.get(0);
		}

		AssertUtil.errorWithBundle("not unique", ServiceException.class);
		return null;
	}
}
